package com.swanclouds.redfish.event;

import com.google.common.base.Preconditions;
import com.swanclouds.api.event.SubscriptionsService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.model.event.Subscriptions;
import com.swanclouds.redfish.common.redfish.EventSubscriptions;
import com.swanclouds.redfish.event.domain.SubscriptionsModel;
import com.swanclouds.redfish.internal.BaseRedfishService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 李文强
 * description 订阅服务实现类
 */
public class SubscriptionsServiceImpl extends BaseRedfishService implements SubscriptionsService {

    /**
     * 查询事件订阅集合资源详情信息
     * @return Subscriptions
     */
    @Override
    public Subscriptions get() {
        return get(SubscriptionsModel.class,uri(ClientConstants.PATH_EVENTSERVICE_SUBSCRIPTIONS)).execute();
    }

    /**
     * 创建订阅事件接口
     * @param subscriptions 所需的参数数据
     * @return Subscriptions
     */
    @Override
    public ActionResponse create(EventSubscriptions subscriptions) {
        Preconditions.checkNotNull(subscriptions, "subscriptions is null");
        Preconditions.checkNotNull(subscriptions.getDestination(),"Destination is null");
        Preconditions.checkNotNull(subscriptions.getEvenTypes(),"EvenTypes is null");
        Preconditions.checkNotNull(subscriptions.getHttpHeaders(),"HttpHeaders is null");
        Preconditions.checkNotNull(subscriptions.getContext(),"Context is null");
        Preconditions.checkNotNull(subscriptions.getProtocol(),"Protocol is null");
        Preconditions.checkNotNull(subscriptions.getMessagelds(),"Messagelds is null");
        Preconditions.checkNotNull(subscriptions.getOriginResources(),"OriginResources is null");
        return post(ActionResponse.class,uri(ClientConstants.PATH_EVENTSERVICE_SUBSCRIPTIONS)).entity(subscriptions).execute();
    }

    /**
     * 查询服务器事件订阅集合资源
     * @return  java.util.List
     **/
    @Override
    public List<SubscriptionsModel> getList() {
        List<SubscriptionsModel> subscriptionsModels = new ArrayList<>();
        Subscriptions subscriptions = get();
        subscriptions.getCollectionResourceList().stream().forEach(ref -> {
            SubscriptionsModel execute = get(SubscriptionsModel.class, uri(ref)).execute();
            subscriptionsModels.add(execute);
        });
        return subscriptionsModels;
    }

    /**
     * 查询事件订阅 创建服务器事件订阅资源
     * @param  subscriptionsModelId 事件订阅资源Id
     * @return  SubscriptionsModel
     **/
    @Override
    public SubscriptionsModel getSubscriptionsModel(String subscriptionsModelId) {
        Preconditions.checkNotNull(subscriptionsModelId, "subscriptionsModelId is null");
        return get(SubscriptionsModel.class,uri(ClientConstants.PATH_EVENTSERVICE_SUBSCRIPTIONS_ID,subscriptionsModelId)).execute();
    }


    /**
     * 删除服务器事件订阅资源
     * @param  subscriptionsModelId 事件订阅资源Id
     * @return  ActionResponse
     **/
    @Override
    public ActionResponse delete(String subscriptionsModelId) {
        Preconditions.checkNotNull(subscriptionsModelId, "Account Id is null");
        return deleteWithResponse(uri(ClientConstants.PATH_EVENTSERVICE_SUBSCRIPTIONS_ID, subscriptionsModelId)).execute();
    }

    @Override
    public Subscriptions update(EventSubscriptions subscriptions) {
        Preconditions.checkNotNull(subscriptions, "subscriptions is null");
        Preconditions.checkNotNull(subscriptions.getDestination(),"Destination is null");
        Preconditions.checkNotNull(subscriptions.getEvenTypes(),"EvenTypes is null");
        Preconditions.checkNotNull(subscriptions.getHttpHeaders(),"HttpHeaders is null");
        Preconditions.checkNotNull(subscriptions.getContext(),"Context is null");
        Preconditions.checkNotNull(subscriptions.getProtocol(),"Protocol is null");
        Preconditions.checkNotNull(subscriptions.getMessagelds(),"Messagelds is null");
        Preconditions.checkNotNull(subscriptions.getOriginResources(),"OriginResources is null");
        return patch(SubscriptionsModel.class,uri(ClientConstants.PATH_EVENTSERVICE_SUBSCRIPTIONS)).entity(subscriptions).execute();
    }
}
